package com.study.login.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.study.exception.DigitalException;
import com.study.login.service.LoginService;
import com.study.model.dto.LoginDTO;
import com.study.model.dto.RegisterDTO;
import com.study.model.vo.LoginResultVO;
import com.study.utils.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@Tag(name = "登录接口")
@RequestMapping("/api/login")
@CrossOrigin
public class LoginController {
    @Autowired
    private LoginService loginService;

//    @GetMapping("/test")
//    @Schema(description = "测试接口")
//    public Result test() {
//        return Result.success("测试接口");
//    }

    @GetMapping("/password")
    @Operation(description = "账户密码登录")
    public Result<LoginResultVO> loginByUserNameAndPassword(
            @RequestParam String username,
            @RequestParam String password,
            HttpServletRequest request,
            HttpServletResponse response
    ){
        try {
            System.out.println("账户密码登录操作");
            LoginDTO userNamePasswordLoginVo = new LoginDTO();
            userNamePasswordLoginVo.setUsername(username);
            userNamePasswordLoginVo.setPassword(password);
            System.out.println(userNamePasswordLoginVo);
            LoginResultVO loginResultVO = loginService.DealloginByUserNameAndPassword(userNamePasswordLoginVo);
            log.info("用户登录成功: {}",userNamePasswordLoginVo.getUsername());
            return Result.success(loginResultVO);
        }catch (DigitalException e) {
            log.warn("用户注册失败: {}-{}",username,e.getMessage());
            return Result.error(e.getCode(),e.getMessage());
        }
    }

    // 实现注册接口的实现,使用账户密码实现登录操作
    @GetMapping("/register")
    @Operation(description = "注册接口")
    public Result<String> register(
            @RequestParam String username,
            @RequestParam String password,
            HttpServletRequest request,
            HttpServletResponse response
    ){
        try {
            System.out.println("账户密码注册操作");
            RegisterDTO registerDTO = new RegisterDTO();
            registerDTO.setUsername(username);
            registerDTO.setPassword(password);
            System.out.println(registerDTO);
            // 传送到service层处理registerDTO
            loginService.RegisterByUserNameAndPassword(registerDTO);
            return Result.success("注册成功");
        }catch (DigitalException e){
            log.warn("用户注册失败: {}-{}",username,e.getMessage());
            return Result.error(e.getCode(),e.getMessage());
        }
    }
    @Operation(description = "登出接口")
    @GetMapping("/logout")
    public Result logout(HttpServletRequest request,
                         HttpServletResponse response) {
        return Result.success("登出成功");
    }
}
